BigDFT.IO module

This module defines some tools used for reading/writing fragment files.

class XYZReader(filename)[source]

Bases: object

A class which can be used to read from xyz files.

This class should behave like a standard file, which means you can use it in with statements, and use the next command to iterate over it.

closed

check if a file is open or not.

Type

bool

units

the units that the xyz file was in.

Type

str

natoms

the number of atoms in the xyz file.

Type

int

cell

a list of floats describing the cell.

Type

list

Parameters

filename (str) – the file to read from. You can also specify a molecule that might be in the database.

close()[source]
property closed
next()[source]
open()[source]
class XYZWriter(filename, natoms, units='angstroem', cell=None)[source]

Bases: object

A class for writing XYZ files.

This class should behave like a standard file, which means you can use it in with statements and write.

Parameters
  • filename (str) – the file to write to.

  • natoms (int) – how many atoms we will write.

  • units (str) – the units of the file. Defaults to angstroem.

  • cell (BigDFT.UnitCells.UnitCell) – the unit cell.

close()[source]
property closed
open()[source]
write(atomdict)[source]

Write an atom to the file.

Parameters

atom (dict) – a dictionary describing the atom.

read_mol2(ifile, disable_warnings=False)[source]

Read a system from a mol2 file.

Parameters
  • ifile (TextIOBase) – the file to read from.

  • disable_warnings (bool) – whether to print warnings about possible file issues.

Returns

the system file.

Return type

(BigDFT.Systems.System)

read_pdb(ifile, include_chain=False, disable_warnings=False, ignore_connectivity=False, ignore_unit_cell=False)[source]

Read a system from a PDB file.

Parameters
  • ifile (TextIOBase) – the file to read from.

  • disable_warnings (bool) – whether to print warnings about possible file issues.

  • include_chain (bool) – include the chain id if True

  • ignore_connectivity (bool) – if True do not store the information about the connectivity matrix.

  • ignore_unit_cell (bool) – assumes free boundary conditions if true.

Warning

This will read in the connectivity information from the pdb as well. However, a pdb file does not provide any information about the bond order. Thus, the bond order of each bond will be set to one.

Returns

the system file.

Return type

(BigDFT.Systems.System)

read_xyz(ifile, fragmentation='atomic')[source]

Read a system from an xyz file. XYZ files do not contain any information about fragments, so this routine will make each atom its own fragment.

Parameters
  • ifile (TextIOBase) – the file to read from.

  • fragmentation (str) – either “atomic” or “single”.

Returns

the system file.

Return type

(BigDFT.Systems.System)

reorder_fragments(frags)[source]
write_mol2(system, ofile)[source]

Write out a system to a string in the mol2 format.

Parameters
  • system (BigDFT.Systems.System) – the system to write.

  • ofile (TextIOBase) – the output stream to write to.

Returns

a string representation of the file.

Return type

(str)

write_pdb(system, ofile)[source]

Write out a system to a string in the pdb format.

Parameters
  • system (BigDFT.Systems.System) – the system to write.

  • ofile (TextIOBase) – the output stream to write to.

write_xyz(system, ofile)[source]

Write out a system to a file in the xyz format.

Parameters
  • system (BigDFT.Systems.System) – the system to write.

  • ofile (TextIOBase) – the output stream to write to.

  • cell (list) – the unit cell.

BigDFT.IO example

Below we show an example of using the IO routines.

def _example():
    """Test the XYZ Module"""
    from BigDFT.Systems import System
    from BigDFT.Fragments import Fragment
    from BigDFT.UnitCells import UnitCell
    file = "Si4"

    safe_print("First let's try reading an XYZ file.")
    atom_list = []
    with XYZReader(file) as reader:
        safe_print(reader.closed)
        for at in reader:
            atom_list.append(at)
    safe_print(reader.closed)
    safe_print(atom_list)
    safe_print()

    safe_print("Now let's try writing an XYZ file.")
    safe_print()
    with XYZWriter("test.xyz", len(atom_list),
                   units=reader.units) as writer:
        safe_print(writer.closed)
        for at in atom_list:
            writer.write(at)
    safe_print(writer.closed)
    safe_print()
    with open("test.xyz") as ifile:
        for line in ifile:
            safe_print(line, end='')
    safe_print()

    safe_print("Print with various boundary conditions")
    with XYZWriter("test.xyz", len(atom_list), reader.units,
                   cell=UnitCell()) as writer:
        for at in atom_list:
            writer.write(at)
    with XYZReader("test.xyz") as ifile:
        print(ifile.cell.get_boundary_condition())

    with XYZWriter("test.xyz", len(atom_list), reader.units,
                   cell=UnitCell([5, 5, 5])) as writer:
        for at in atom_list:
            writer.write(at)
    with XYZReader("test.xyz") as ifile:
        print(ifile.cell.get_boundary_condition())

    with XYZWriter("test.xyz", len(atom_list), reader.units,
                   cell=UnitCell([5, float("inf"), 5])) as writer:
        for at in atom_list:
            writer.write(at)
    with XYZReader("test.xyz") as ifile:
        print(ifile.cell.get_boundary_condition())

    with XYZWriter("test.xyz", len(atom_list), reader.units,
                   cell=UnitCell([float("inf"), float("inf"), 5])) as writer:
        for at in atom_list:
            writer.write(at)
    with XYZReader("test.xyz") as ifile:
        print(ifile.cell.get_boundary_condition())
    safe_print()

    safe_print("Now let's demonstrate the pdb and mol2 writer")
    sys = System()
    sys["FRAG:0"] = Fragment(atom_list)
    with open("test.pdb", "w") as ofile:
        write_pdb(sys, ofile)
    with open("test.pdb") as ifile:
        for line in ifile:
            safe_print(line, end='')
    safe_print()

    with open("test.mol2", "w") as ofile:
        write_mol2(sys, ofile)
    with open("test.mol2") as ifile:
        for line in ifile:
            safe_print(line, end='')
    safe_print()